<template>
  <!--授权信息  -->
	<div>
		<FormPanel
			ref="FormPanel"
			:list="initForm"
			:cols="2"
			:formType="formType"
			:labelWidth="180"
			:isSubmitBtn="false"
			@formChange="formChangeHandle"
			class="mtop"
		/>
	</div>
</template>

<script>
	import { dictionary } from '@/utils/dictMixin';
	import * as utils from '@/utils';
	import * as types from '@/api/cms-api/origin/originInfo/types';

	import vm from './bridge';
	import MP from '@/assets/js/map';  //地图

	export default {
		mixins: [dictionary],
		model:{
			prop:'pdata',//为了和v-model默认的value区分，将prop改为自定义的名称
			event:'click'//为了和v-model默认的input区分，将event改为click
		},
		props: {
			pdata: {
				type: Object,
				default: () => ({})
			},
			formType:{
				type: String,
				required:true
			}
		},
		data() {
			return {
				initForm: this.initFormFun()
			};
		},
		mounted(){
			//注册经销商类型变化时触发的事件
			//在审核时，给此页面的类型为只读，但是基本信息（infoBase可以编辑，会显示会触发事件，事件会触发禁用取消，覆盖整个表单的类型（表单类型为show，但是依旧可以输入，（部分）））
			if(this.formType != "show"){
				vm.$on('changeDealerType',this.changeDealerType);
			}
		},
		methods: {
			//初始化数据（由外部调用）
			init() {
				this.initForm.forEach(x => {
					// if(x.type === "DATE"){
					// 	x.initialValue = this.pdata[x.fieldName] = utils.formatdateByTime(this.pdata[x.fieldName]);
					// }else{
					//如果是‘电池联合服务中心’则需要特殊处理一下，否则会出现类型不一致导致回显出现问题
					if(x.fieldName === 'batteryServiceStation'){
						x.initialValue = Number(this.pdata[x.fieldName]);
					}else{
						x.initialValue = this.pdata[x.fieldName];
					}
					// }
				});
			},
			//销售获取经纬度
			saleAdress(){
				let self = this;
				//获取地址
				let salesAddress = this.initForm.find(x => x.fieldName === 'salesAddress').initialValue ;
				if(salesAddress){
					var city ="";
					if(salesAddress.indexOf("北京")>=0){
						city ="北京市";
					}else if(salesAddress.indexOf("上海")>=0){
						city ="上海市";
					}else if(salesAddress.indexOf("天津")>=0){
						city ="天津市";
					}else if(salesAddress.indexOf("重庆")>=0){
						city ="重庆市";
					}else if(salesAddress.indexOf("内蒙古")>=0){
						city ="内蒙古自治区";
					}else if(salesAddress.indexOf("宁夏")>=0){
						city ="宁夏回族自治区";
					}else if(salesAddress.indexOf("新疆")>=0){
						city ="新疆维吾尔自治区";
					}else if(salesAddress.indexOf("西藏")>=0){
						city ="西藏自治区";
					}else if(salesAddress.indexOf("广西")>=0){
						city ="广西壮族自治区";
					}else if(salesAddress.indexOf("香港")>=0){
						city ="香港";
					}else if(salesAddress.indexOf("澳门")>=0){
						city ="澳门";
					}else if(salesAddress.indexOf("省")>=0){
						city = salesAddress.substring(0,salesAddress.indexOf("省")+1); 
					}else{
						this.$notify.warning({title: '提示信息',message:'请输入正确的销售运营地址!'});
						return;
					}

					MP().then(BMap => {
						var map = new BMap.Map("allmap");      
						// 创建地址解析器实例     
						var myGeo = new BMap.Geocoder();   
						// 获取地址经纬度     
						myGeo.getPoint(salesAddress, function(point,e){  
							if (e) {      
								self.initForm.find(x => x.fieldName === 'salesLongitude').initialValue =  point.lng;
								self.initForm.find(x => x.fieldName === 'salesLatitude').initialValue =  point.lat;
							}else{
								self.$notify.warning({title: '提示信息',message:'请输入正确的销售运营地址!'});
							}
						}, 
						city);
					});
				}else{
					this.$notify.warning({title: '提示信息',message:'请输入销售运营地址!'});
				}

			},
			formChangeHandle(val) {
				this.pdata = Object.assign(this.pdata,val);
				//传递数据到父组件里
				this.$emit('click', this.pdata);
				//触发外部的事件，以标志着本表单已经准备完毕
				this.$emit('formchange', this.pdata);
			},
			//开放的保存方法
			saveDialog(){
				this.$refs.FormPanel.SUBMIT_FORM();
			},
			//经销商类型变更时
			changeDealerType(value){
				if(types.DEALER_INFO_DEALER_TYPE_SINGLE_AFTER_SALE === value){
					//如果是单一售后
					//禁用销售模块
					this.saleInfoDisabled();
					//启用售后模块
					this.afterSaleEnabled();
				}else if(types.DEALER_INFO_DEALER_TYPE_SINGLE_SALE === value){
					//如果是单一销售
					//禁用售后模块
					this.afterSaleDisabled();
					//启用销售模块
					this.saleInfoEnabled();
				}else if(types.DEALER_INFO_DEALER_TYPE_4S === value){
					//如果是4S店则开启全部
					this.saleInfoEnabled();
					this.afterSaleEnabled();
				}else{
					this.saleInfoEnabled();
					this.afterSaleEnabled();
				}
				// this.refreshFormRules();
			},
				//售后获取经纬度
			buyAdress(){
				let self = this;
				//获取地址
				let serviceAddress = this.initForm.find(x => x.fieldName === 'serviceAddress').initialValue ;
				if(serviceAddress){
					var city ="";
					if(serviceAddress.indexOf("北京")>=0){
						city ="北京市";
					}else if(serviceAddress.indexOf("上海")>=0){
						city ="上海市";
					}else if(serviceAddress.indexOf("天津")>=0){
						city ="天津市";
					}else if(serviceAddress.indexOf("重庆")>=0){
						city ="重庆市";
					}else if(serviceAddress.indexOf("内蒙古")>=0){
						city ="内蒙古自治区";
					}else if(serviceAddress.indexOf("宁夏")>=0){
						city ="宁夏回族自治区";
					}else if(serviceAddress.indexOf("新疆")>=0){
						city ="新疆维吾尔自治区";
					}else if(serviceAddress.indexOf("西藏")>=0){
						city ="西藏自治区";
					}else if(serviceAddress.indexOf("广西")>=0){
						city ="广西壮族自治区";
					}else if(serviceAddress.indexOf("香港")>=0){
						city ="香港";
					}else if(serviceAddress.indexOf("澳门")>=0){
						city ="澳门";
					}else if(serviceAddress.indexOf("省")>=0){
						city = serviceAddress.substring(0,serviceAddress.indexOf("省")+1); 
					}else{
						this.$notify.warning({title: '提示信息',message:'请输入正确的售后运营地址!'});
						return;
					}

					MP().then(BMap => {
						var map = new BMap.Map("allmap");      
						// 创建地址解析器实例     
						var myGeo = new BMap.Geocoder();   
						// 获取地址经纬度   
						myGeo.getPoint(serviceAddress, function(point,e){      
							if (e) {      
								self.initForm.find(x => x.fieldName === 'serviceLongitude').initialValue =  point.lng;
								self.initForm.find(x => x.fieldName === 'servicesLatitude').initialValue =  point.lat;
							}else{
								self.$notify.warning({title: '提示信息',message:'请输入正确的售后运营地址!'});
							}
						}, 
						city);
					});
				}else{
					this.$notify.warning({title: '提示信息',message:'请输入售后运营地址!'});
				}

			},
			//禁用销售部分
			saleInfoDisabled(){
				vm.saleInfoDisabled(this.initForm);
			},
			//启用销售部分
			saleInfoEnabled(){
				vm.saleInfoEnabled(this.initForm);
			},
			//禁用售后部分
			afterSaleDisabled(){
				vm.afterSaleDisabled(this.initForm);
			},
			//启用售后部分
			afterSaleEnabled(){
				vm.afterSaleEnabled(this.initForm);
			},
			//刷新验证内容
			// refreshFormRules(){
			// 	vm.refreshFormRules(this,this.$refs.FormPanel,this.initForm);
			// },
			//搜索数据
			initFormFun() {
				return [
					{type:'DATE',label:'授权时间',fieldName:'authTime',valueFormat:'yyyy-MM-dd',initialValue:'',placeholder:'',style:{width:'100%'},disabled:false},
					{type:'DATE',label:'验收通过时间',fieldName:'passTime',valueFormat:'yyyy-MM-dd',initialValue:'',placeholder:'',style:{width:'100%'},disabled:false},
					{type:'DATE',label:'入网通过时间',fieldName:'joinTime',valueFormat:'yyyy-MM-dd',initialValue:'',placeholder:'',style:{width:'100%'},disabled:false},
					{type:'SELECT',label:'销售店面级别',fieldName:'saleLevel',placeholder:'',initialValue:'',itemList:this.createDictList('2542'),disabled:false},
					{type:'INPUT_NUMBER',label:'展示面积',fieldName:'exaibitionRoomArean',initialValue:undefined,placeholder:'',precision: 2,max:99999999.99,min:0,disabled:false},
					{type:'INPUT_NUMBER',label:'办公面积',fieldName:'officeArea',initialValue:undefined,placeholder:'',precision: 2,max:99999999.99,min:0,disabled:false},
					{type:'INPUT_NUMBER',label:'展厅面宽',fieldName:'exaibitionRoomWidth',initialValue:undefined,placeholder:'',precision: 2,max:99999999.99,min:0,disabled:false},
					{type:'INPUT',label:'销售店经度',fieldName:'salesLongitude',initialValue:'',placeholder:'',maxlength:60,disabled:false},
					{type:'INPUT',label:'销售店纬度',fieldName:'salesLatitude',initialValue:'',placeholder:'',maxlength:60,disabled:false},
					{type:'INPUT',label:'销售运营地址',fieldName:'salesAddress',selfCols: 2,rows:1,initialValue:'',placeholder:'',maxlength:200,disabled:false,
						unitRender:()=>{
							return <el-button type="text" onClick={() => this.saleAdress()}>获取经纬度</el-button>
						}
					},
					{type:'INPUT_NUMBER',label:'工位数量',fieldName:'stationNum',initialValue:undefined,placeholder:'',precision: 0,max:99999999.99,min:0,},
					{type:'SELECT',label:'售后店面级别',fieldName:'serviceLevel',placeholder:'',initialValue:'',itemList:this.createDictList('2543'),disabled:false},
					{type:'INPUT_NUMBER',label:'工时系数',fieldName:'laborCoefficient',initialValue:undefined,placeholder:'',precision: 2,max:99999999.99,min:0,disabled:false},
					{type:'SELECT',label:'样板店',fieldName:'isModelShop',placeholder:'',initialValue:'',itemList:this.createDictList('1004'),disabled:false},
					{type:'SELECT',label:'动力电池回收网络',fieldName:'isPowerRecycing',placeholder:'',initialValue:'',itemList:this.createDictList('1004'),disabled:false},
					{type:'SELECT',label:'电池联合服务中心',fieldName:'batteryServiceStation',initialValue:'',placeholder:'',maxlength:300,disabled:false,itemList:this.createDictList('1004'),},
					{type:'INPUT',label:'与销售店的距离',fieldName:'distanceBetweenSale',initialValue:'',placeholder:'',maxlength:8,disabled:false,
						unitRender:()=>{return<span>(M)</span>;},
					},
					{type:'INPUT',label:'售后店经度',fieldName:'serviceLongitude',initialValue:'',placeholder:'',maxlength:60,disabled:false},
					{type:'INPUT',label:'售后店纬度',fieldName:'servicesLatitude',initialValue:'',placeholder:'',maxlength:60,disabled:false},
					{type:'INPUT_NUMBER',label:'车间面积',fieldName:'workshopArea',initialValue:undefined,placeholder:'',precision: 2,max:99999999.99,min:0,disabled:false},
					{type:'INPUT',label:'售后运营地址',fieldName:'serviceAddress',selfCols: 2,rows:1,initialValue:'',placeholder:'',disabled:false,
						unitRender:()=>{
							return <el-button type="text" onClick={() => this.buyAdress()}>获取经纬度</el-button>
						}
					},

				];
			},
		}
	};
</script>